package com.dl.controller;

import com.dl.dao.StudentMapper;
import com.dl.pojo.Student;
import com.dl.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    //action
    @RequestMapping("/regist.action")
    public ModelAndView regist(Student student,ModelAndView mav) throws IOException {

        int count = studentService.addStudent(student);

        if(count>0){
            //insert success
            mav.addObject("student",student);
            mav.setViewName("success.jsp");
        }else{
            //failed
            mav.setViewName("fail.jsp");
        }
        return mav;
    }

    @RequestMapping("/update.action")
    public ModelAndView update(){
        System.out.println("更新成功！");

        ModelAndView mav=new ModelAndView();
        mav.setViewName("/success.jsp");
        return mav;
    }

    //返回字符串类型
    @RequestMapping("/reStr.action")
    public String reStr(HttpServletRequest request, ModelAndView mav){

        System.out.println("reStr::::");

        //request.setAttribute("name","LS");    携带数据

        return "success.jsp";   //请求转发
        //return "redirect:success.jsp";  //重定向
    }

    //查询所有学生
    @RequestMapping("/showStus.action")
    public ModelAndView showStus(ModelAndView mav){
        List<Student> stus = studentService.getStus();

        mav.addObject("stus",stus);  //传递数据
        mav.setViewName("showStus.jsp");
        return mav;
    }
}
